/***** HistogramView.m -- histogram data display object implementation
	NeXTstep Measurement Kit
	by Alex Meyer <ameyer@phoenix.Princeton.EDU>
	for computer science senior thesis
	19 April 1992 -- created
*****/

#import <stdlib.h>
#import <dpsclient/wraps.h>
#import "HistogramView.h"

@implementation HistogramView

- drawBar:(int)ind
	inRect:(NXRect *)rect
{
	double fraction;
	NXRect myRect;

	myRect = *rect;
	NXInsetRect(&myRect,5,0);
	myRect.size.height -= 5;
	fraction = ((double) data[ind] - smallest) / (largest - smallest);
	myRect.size.height *= fraction;
	PSsetgray(NX_DKGRAY);
	NXRectFill(&myRect);
	return (self);
}


- drawSelf:(const NXRect *)rects	/* should only draw what's needed */
	:(int)rectCount
{
	int i;
	NXRect bigRect,smallRect;

	PSsetgray(NX_LTGRAY);
	NXRectFill(&bounds);
	bigRect = bounds;
	NXInsetRect(&bigRect,5,5);
	NXDrawWhiteBezel(&bigRect,&bounds);
	NXInsetRect(&bigRect,5,5);
	PSnewpath();
	PSmoveto(bigRect.origin.x,bigRect.origin.y);
	PSrlineto(bigRect.size.width,0);
	PSclosepath();
	PSsetgray(NX_BLACK);
	PSstroke();
	smallRect = bigRect;
	smallRect.size.width /= numData;
	for (i = 0;i < numData;++i) {
		[self drawBar:i
			inRect:&smallRect];
		smallRect.origin.x += smallRect.size.width;
	}
	return (self);
}


- copyInNum:(int)num
	data:(unsigned *)ptr
{
	int i;

	numData = num;
	data = malloc(num * sizeof(*data));
	largest = 0;
	smallest = 0;
	for (i = 0;i < num;++i) {
		data[i] = ptr[i];
		if (data[i] > largest)
			largest = data[i];
		if (data[i] < smallest)
			smallest = data[i];
	}
	return (self);
}

@end
